﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class addNewCourseForm : Form
    {
        Form1 form1;
        ServiceReference1.DatabaseClient test;
        public struct kurs
        {
            public int id;
            public string code;
            public string name;

            public kurs(int id, string code, string name)
            {
                this.id = id;
                this.code = code;
                this.name = name;
            }
        }
        List<kurs> kurser = new List<kurs>();

        public addNewCourseForm(ServiceReference1.DatabaseClient test, Form1 form1)
        {
            InitializeComponent();
            code_txt.Text = "";
            name_txt.Text = "";
            
            this.test = test;
            this.form1 = form1;

        }
        private void addNewCourseForm_Load(object sender, EventArgs e)
        {
            string[][] databaseInput = test.GetCourses();
            kurser.Clear();
            courses_combo.Items.Clear();
            for (int i = 0; i < databaseInput.Length; i++)
            {
                kurser.Add(new kurs(int.Parse(databaseInput[i][0]), databaseInput[i][1], databaseInput[i][2]));
            }

            foreach (kurs k in kurser)
            {
                courses_combo.Items.Add(k.code.Trim() + "\t"+" " + k.name);
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            bool check = checkSomeShit();
            if (check)
            {
                if (courses_combo.Enabled == true)
                {
                    string oldCode = courses_combo.SelectedItem.ToString();
                    string[] words = oldCode.Split('\t');
                    oldCode = words[0].Trim();

                    Console.WriteLine(oldCode);
                    //newCode = test.AddCourse(code_txt.Text, name_txt.Text);

                    //test.CopyCourse(newCode, oldCode);
                }
                else
                {
                    test.AddCourse(code_txt.Text.ToUpper(), name_txt.Text);
                }
                form1.Form1_Load(this, e);
                this.Close();
            }

        }

        private bool checkSomeShit()
        {

            if (courses_combo.SelectedItem == null && copyCourse_chk.Checked==true) return false;
            code_txt.BackColor = Color.White;
            name_txt.BackColor = Color.White;

            warning_lbl1.Text = "";
            warning_lbl2.Text = "";

            if (code_txt.Text == "")
            {
                code_txt.Select();
                code_txt.BackColor = Color.Orange;
                warning_lbl1.Text = "!";
                return false;
            }
            if (name_txt.Text == "")
            {
                name_txt.Select();
                name_txt.BackColor = Color.Orange;
                warning_lbl2.Text = "!";
                return false;
            }

            try
            {
                return true;
            }
            catch (Exception e)
            {
                e = null;
            }
            System.Windows.Forms.MessageBox.Show("FU!");
            return false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void copyCourse_chk_CheckedChanged(object sender, EventArgs e)
        {
            if (copyCourse_chk.Checked == true)
            {
                courses_combo.Visible = true;
                courses_combo.Enabled = true;
            }
            if (copyCourse_chk.Checked == false)
            {
                courses_combo.Visible = false;
                courses_combo.Enabled = false;
            }
        }

        private void courses_combo_EnabledChanged(object sender, EventArgs e)
        {
            if (courses_combo.Enabled == true)
            {
                if (kurser.Count == 0)
                {
                    courses_combo.Items.Clear();
                    kurser.Clear();
                    form1.Form1_Load(this, e);
                }
            }
        }
    }
}
